package com.wbs.shop.nuby;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;

import androidx.lifecycle.ProcessLifecycleOwner;
import androidx.multidex.MultiDex;

import com.bumptech.glide.request.target.ViewTarget;
import com.kingja.loadsir.callback.SuccessCallback;
import com.kingja.loadsir.core.LoadSir;
import com.qlmy.cn.helper.ProcessLifecycleObserver;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;
//import com.umeng.commonsdk.UMConfigure;
import com.wbs.shop.nuby.api.ApiHelper;
import com.wbs.shop.nuby.helper.EmptyCallback;
import com.wbs.shop.nuby.helper.ErrorCallback;
import com.wbs.shop.nuby.utils.ActivityManger;
import com.wbs.shop.nuby.utils.GlideAlbumLoader;
import com.wbs.shop.nuby.utils.Utils;
import com.wbs.shop.nuby.utils.push.JPushUtils;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by zhoucl on 2018/4/8 0008.
 */
public class App extends Application {
    public static App app;

    @Override
    public void onCreate() {
        super.onCreate();
        String processName = getProcessName(this);
        if (getPackageName().equals(processName)) {
            init();
        }
    }

    public static App getApp() {
        return app;
    }

    private void init() {
        app = this;
//        XWalkPreferences.setValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW, true);
        Utils.init(this);
        Album.initialize(
                AlbumConfig.newBuilder(this)
                        .setAlbumLoader(new GlideAlbumLoader()) // 设置Album加载器。
                        .setLocale(Locale.CHINA) // 比如强制设置在任何语言下都用中文显示。
                        .build()
        );
        JPushUtils.init(this);
        JPushInterface.setDebugMode(BuildConfig.DEBUG);
        JPushInterface.init(this);
//        UMConfigure.setLogEnabled(BuildConfig.isDebug);
//        UMConfigure.init(this, "58edcfeb310c93091c000be2", "umeng", UMConfigure.DEVICE_TYPE_PHONE, null);//58edcfeb310c93091c000be2 5965ee00734be40b580001a0
        //PlatformConfig.setWeixin("wx20c8b24611bab022", "3bc2f3a588b60ad785b6504ea9e99b8c");
        CrashReport.initCrashReport(getApplicationContext(), "bf6a93dc3a", false);
        ActivityManger.registerActivityLifecycleCallbacks(this);
        LoadSir.beginBuilder()
                .addCallback(new EmptyCallback())
                .addCallback(new ErrorCallback())
//                .addCallback(new LoadingCallback())
                .setDefaultCallback(SuccessCallback.class)//设置默认状态页
                .commit();
        ViewTarget.setTagId(R.id.adapter_item_tag_key);
        // 在调用TBS初始化、创建WebView之前进行如下配置
        HashMap<String, Object> map = new HashMap<>();
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
        QbSdk.initTbsSettings(map);
        ProcessLifecycleOwner.get().getLifecycle().addObserver(ProcessLifecycleObserver.INSTANCE);
    }

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            layout.setPrimaryColorsId(R.color.bg, R.color.text_color);//全局设置主题颜色
            return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header，默认是 贝塞尔雷达Header
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> {
            //指定为经典Footer，默认是 BallPulseFooter
            layout.setPrimaryColorsId(R.color.bg, R.color.text_color);//全局设置主题颜色
            return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
        });
    }

    public void exit() {
        ActivityManger.exit();
    }

    public void finishAllActivity() {
        ActivityManger.finishAllActivity();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    private String getProcessName(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo proInfo : runningApps) {
            if (proInfo.pid == android.os.Process.myPid()) {
                if (proInfo.processName != null) {
                    return proInfo.processName;
                }
            }
        }
        return null;
    }
}
